package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.OrderDetailService;
import cn.bytecollege.admin.service.OrderService;
import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Orders;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author NJK
 */
@WebServlet(name = "OrderAddController", value = "/admin/orderAdd")
public class OrderAddController extends HttpServlet {
    private OrderService orderService = null;
    private OrderDetailService orderDetailService = null;
    public OrderAddController(){
        orderService = new OrderService();
        orderDetailService = new OrderDetailService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        String orderNumber = request.getParameter("orderNumber");

        Double totalPrice= orderDetailService.calculatePriceByOrderNumber(orderNumber);
        LocalDateTime createTime = LocalDateTime.now();
        //封装数据
        Orders orders = new Orders();
        orders.setOrderNumber(orderNumber);
        orders.setTotalPrice(totalPrice);
        orders.setCreateTime(createTime);

        orderService.save(orders);

        response.sendRedirect(request.getContextPath()+"/admin/orderList?payFlag=0");

    }
}
